# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ruby-pg_query
_gemname=pg_query
# Keep in mind the aport libpg_query when bumping.
pkgver=5.1.0
pkgrel=0
pkgdesc="PostgreSQL query parsing and normalization library for Ruby"
url="https://github.com/pganalyze/pg_query"
# x86, armhf, armv7: fingerprint feature is broken on 32-bit
# s390x: Failing tests
# riscv64 blocked by libpg_query
arch="all !x86 !armhf !armv7 !s390x !riscv64"
license="BSD-3-Clause"
depends="ruby ruby-google-protobuf>=3.15.5"
checkdepends="ruby-rspec"
makedepends="
	libpg_query-dev
	postgresql16-dev
	protoc
	ruby-dev
	xxhash-dev
	"
source="https://github.com/pganalyze/pg_query/archive/v$pkgver/$pkgname-$pkgver.tar.gz
	unbundle-deps.patch
	no-simplecov.patch
	"
builddir="$srcdir/$_gemname-$pkgver"

prepare() {
	default_prepare

	# Remove bundled dependencies.
	cd ext/pg_query
	rm protobuf-c.c xxhash.c src_*.c src_*.h
	find . -name 'pg_query*' ! -name 'pg_query_ruby.*' -delete
	rm -Rf include
}

build() {
	# Regenerate Ruby source from pg_query.proto provided by libpg_query
	# package - to be sure that we use correct version.
	protoc --proto_path=/usr/include/pg_query \
		--ruby_out=lib/pg_query \
		/usr/include/pg_query/pg_query.proto

	gem build $_gemname.gemspec

	CFLAGS="$CFLAGS -I$(pg_config --includedir-server)" \
	gem install \
		--local \
		--install-dir dist \
		--ignore-dependencies \
		--no-document \
		--verbose \
		$_gemname

	# Needed for running tests.
	cp -l dist/extensions/*/*/$_gemname-*/$_gemname/*.so lib/$_gemname/
}

check() {
	rspec --format d
}

package() {
	local gemdir="$pkgdir/$(ruby -e 'puts Gem.default_dir')"
	cd "$builddir"/dist

	mkdir -p "$gemdir"
	cp -r extensions gems specifications "$gemdir"/

	# Remove unnecessary and duplicated files.
	rm -f "$gemdir"/extensions/*/*/$_gemname-*/mkmf.log
	cd "$gemdir"/gems/$_gemname-$pkgver
	rm -rf ext/ lib/$_gemname/*.so LICENSE Rakefile *.md
}

sha512sums="
3c14637b97e13dee9930fa408dc9f654c034751bccf5bb4da26771d3860d23e16b42d83585cd26a269ea57e4f6dee837ea8645cec26583ea09d905e4a033936e  ruby-pg_query-5.1.0.tar.gz
bf510c52b9fd2e70a7d0dda51a55f73f825c8bb4904efc4604bffece82efcf8d75a7fe11148bb2cac598491196a9c8e0aeded6bd5ae78d2746e3b85b749d0c77  unbundle-deps.patch
42addad0f327b988eff0ff39f4a80d999211ac1c2d168b00143957146539a4400494447b22830e44a4d6484619c8f4dbc22fc6c1b094f3cebca9b4b7dd181da6  no-simplecov.patch
"
